package com.farm.test.http;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.farm.wcp.client.utils.HttpUtils;


/**通用接口测试
 * @author macpl
 *
 */
public class RestFulApiTester {

	public static String baseUrl = "http://127.0.0.1:8090/wcp/";
	public static String secret = "F59BD65F7EDAFB087A81D4DCA06C4910";
	public static String testOrgId = null;
	public static String testUserId = null;
	public static String operatorLoginname = "ApiUser";
	public static String operatorPassword = "111111";

	public static void main(String[] args) {
		// 参数查询
		test01();
		// 参数设置
		test02();

		// 组织机构查询
		test1();
		// 创建组织机构
		test2();
		// 更新组织机构
		test3();
		// 删除组织机构
		test4();

		// 岗位查询 岗位创建 岗位更新 岗位删除 岗位绑定人员
		test45();

		// 用户查询
		test5();
		// 创建用户
		test6();
		// 更新用户
		test7();
		// 删除用户
		test8();

		// 分类查询
		test9();
		// 知识查询
		test10();
		// 问答查询
		test11();
		// 知识详情
		test121("402880e88c8b13f2018c8b68d023050a");
		// 问答详情
		test122("402880e88c804a4d018c804bddea0002");
		// 全问检索
		test12();

		// 免密登录
		test13();
	}

	public static void checkState(JSONObject json) {
		String state = json.get("STATE").toString();
		if (state.equals("1")) {
			throw new RuntimeException(json.getString("MESSAGE"));
		}
	}

	public static void test121(String docid) {
		System.out.println("知识详情查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("id", docid);
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/view/know.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test122(String fqaid) {
		System.out.println("问答详情查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("id", fqaid);
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/view/fqa.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test01() {
		System.out.println("参数查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("key", "config.able.booktype");
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/get/parameter.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test02() {
		System.out.println("参数设置");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("key", "config.able.booktype");
		data.put("val", "false");
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/put/parameter.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test1() {
		System.out.println("组织机构查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/get/organization.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test2() {
		System.out.println("创建组织机构");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("sort", "1");
		data.put("name", "testOrg");
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/post/organization.do", data);
		checkState(obj);
		System.out.println(obj.getString("ID"));
		testOrgId = obj.getString("ID");
	}

	public static void test3() {
		System.out.println("更新组织机构");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("id", testOrgId);
		data.put("comments", "update");
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/put/organization.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test4() {
		System.out.println("删除组织机构");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("id", testOrgId);
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/delete/organization.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	/**
	 * 岗位增删改查
	 */
	public static void test45() {
		System.out.println("岗位增删改查");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		data.put("sourceid", "testpost");
		// ----------------------------------------
		{
			data.put("name", "testpost");
			data.put("extendis", "2");
			JSONObject obj1 = HttpUtils.httpPost(baseUrl + "api/post/post.do", data);
			checkState(obj1);
			System.out.println("创建" + obj1);
		}
		{
			data.put("name", "testpost1");
			JSONObject obj1 = HttpUtils.httpPost(baseUrl + "api/put/post.do", data);
			checkState(obj1);
			System.out.println("更新" + obj1);
		}
		{

			data.put("sourceflag", "testpost");
			JSONObject obj1 = HttpUtils.httpPost(baseUrl + "api/get/posts.do", data);
			checkState(obj1);
			System.out.println("查询" + obj1);
		}

		{

			data.put("sourceids", "testpost");
			data.put("loginname", "sysadmin");
			JSONObject obj1 = HttpUtils.httpPost(baseUrl + "api/bind/posts.do", data);
			checkState(obj1);
			System.out.println("绑定人员" + obj1);
		}
		{
			JSONObject obj1 = HttpUtils.httpPost(baseUrl + "api/delete/post.do", data);
			checkState(obj1);
			System.out.println("删除" + obj1);
		}
	}

	public static void test5() {
		System.out.println("用户查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/get/user.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test6() {
		System.out.println("创建用户");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("name", secret);
		data.put("loginname", secret);
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/post/user.do", data);
		checkState(obj);
		testUserId = obj.getString("ID");
		System.out.println(obj);
	}

	public static void test7() {
		System.out.println("更新用户");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("id", testUserId);
		data.put("comments", "update");
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/put/user.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test8() {
		System.out.println("删除用户");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("id", testUserId);
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/delete/user.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test9() {
		System.out.println("分类查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/get/category.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test10() {
		System.out.println("知识查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/get/know.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test11() {
		System.out.println("问答查询");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/get/fqa.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test12() {
		System.out.println("全文检索");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("word", "知识");
		data.put("domtype", "all");
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/search.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test13() {
		System.out.println("免密登陸");
		Map<String, String> data = new HashMap<>();
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		data.put("secret", secret);
		// ----------------------------------------
		data.put("loginname", "sysadmin");
		System.out.println("通过登录名" + "sysadmin" + "换取注册码...");
		JSONObject obj = HttpUtils.httpPost(baseUrl + "api/regist/login.do", data);
		String loginCode = obj.get("CERTIFICATE").toString();
		System.out.println("注册码为：" + loginCode);
		// -----------------------------------------------------------------------------------------
		Map<String, String> data2 = new HashMap<>();
		data2.put("secret", secret);
		data2.put("certificate", loginCode);
		System.out.println("通过注册码" + loginCode + "换取登录名 ...");
		JSONObject objs = HttpUtils.httpPost(baseUrl + "api/get/login.do", data2);
		try {
			checkState(objs);
			System.out.println("换取的登录名为：" + objs.getString("LOGINNAME"));
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
